package com.example.padlock.view.sendvedio;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;

import android.widget.Toast;

import com.google.gson.Gson;

public class ConnectThread extends Thread {

	private String mIP;
	private int mPort;
	private Socket mSocket;
	private OutputStream os = null;
	private InputStream is = null;
	// 是否可以发送图像
	private boolean isSendAble = false;
	private OnSendListener mListener;

	public ConnectThread(String ip, int port) {
		this.mIP = ip;
		this.mPort = port;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		connect();
		if (mSocket != null) {
			try {
				os = mSocket.getOutputStream();
				PadEntity entity = new PadEntity();
				entity.setMapId("123456");
				entity.setsType("v-pad");
				entity.setTgKey("v-phone");
				String json = new Gson().toJson(entity);

				if (os != null) {
					BufferedWriter writer = new BufferedWriter(
							new OutputStreamWriter(os));
					writer.write(json);
					writer.newLine();
					writer.flush();
				}
				//mSocket.shutdownOutput();
				is = mSocket.getInputStream();
				byte[] buffer=new byte[100];
				byte[] using;
				int length=-1;
				String data=null;
				while((length=is.read(buffer))!=-1){
					using=Arrays.copyOfRange(buffer, 0, length);
					data=new String(using,"UTF-8");
					System.out.println("socketState11:"+data);
					System.out.println("socketState11:"+data.length());
					if (data.equals("200")){
						System.out.println("issend:...........");
						isSendAble = true;
						break;
					} else {
						isSendAble = false;
						break;
					}
				}
				
//				System.out.println("socketState1："+is.read(buffer));
//				BufferedReader reader=new BufferedReader(new InputStreamReader(is));
//				System.out.println("socketState0："+reader.readLine());
//				while((data=reader.readLine())!=null){
//					System.out.println("socketState："+data);
//				}
				//mSocket.shutdownInput();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				disConnect();
			}
		}
	}

	public void disConnect() {
		try {
			if (mSocket != null) {
				mSocket.close();
				mSocket = null;
			}
			if (os != null) {
				os.close();
				os = null;
			}
			if (is != null) {
				is.close();
				is = null;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void write(byte[] buf, int offset, int count) {
		if (os != null) {
			try {
				os.write(buf, offset, count);
				os.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				disConnect();
			}
		}
	}

	private void connect() {
		try {
			mSocket = new Socket(mIP, mPort);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean isSendAble() {
		return isSendAble;
	}

	public void setOnSendListener(OnSendListener listener) {
		this.mListener = listener;
	}

	public interface OnSendListener {
		public void onSend(OutputStream os);
	}

}
